package cn.stephen.study.demoproject.service.impl;

import cn.stephen.study.demoproject.dao.dbone.PatrolMapper;
import cn.stephen.study.demoproject.dao.dbone.PatrolValueMapper;
import cn.stephen.study.demoproject.entity.Patrol;
import cn.stephen.study.demoproject.entity.PatrolValue;
import cn.stephen.study.demoproject.service.PatrolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class PatrolServiceImpl implements PatrolService {

    @Autowired
    private PatrolMapper patrolMapper;
    @Autowired
    private PatrolValueMapper patrolValueMapper;

    @Override
    public List<Patrol> findAllByValue(String startTime, String endTime, String stationCode) {
        if (startTime != null && endTime == null) {
            startTime = startTime + " 00:00:00";
            return patrolMapper.findAllByValue(startTime, endTime, stationCode);
        }
        if (endTime != null && startTime == null) {
            endTime = endTime + " 23:59:59";
            return patrolMapper.findAllByValue(startTime, endTime, stationCode);
        }
        if (endTime != null && startTime != null) {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
            return patrolMapper.findAllByValue1(startTime, endTime, stationCode);
        }
        return patrolMapper.findAllByValue(startTime, endTime, stationCode);
    }

    @Override
    public int savePatrol(Patrol patrol) {
        if (patrol.getId() == null) {
            //新增
            String uuid = UUID.randomUUID().toString();
            patrol.setId(uuid);
            patrolMapper.insetPatrol(patrol);
            //新增巡查值
            PatrolValue patrolValue = new PatrolValue();
            patrolValue.setPatrolId(patrol.getId());
            patrolValue.setPatrol1(patrol.getNI());
            patrolValue.setPatrol2(patrol.getNO());
            patrolValue.setPatrol3(patrol.getEI());
            patrolValue.setPatrol4(patrol.getEO());
            patrolValue.setPatrol5(patrol.getHI());
            patrolValue.setPatrol6(patrol.getHO());
            patrolValue.setPatrol7(patrol.getDN());
            patrolValue.setPatrol8(patrol.getDE());
            patrolValue.setPatrol9(patrol.getDF());
            patrolValue.setPatrol10(patrol.getDH());
            patrolValue.setPatrol11(patrol.getDG());
            patrolValueMapper.insetPatrol(patrolValue);
        } else {
            //修改
            patrolMapper.updatePatrol(patrol);
            PatrolValue patrolValue = patrolValueMapper.findAllByValue(patrol.getId());
            patrolValue.setPatrol1(patrol.getNI());
            patrolValue.setPatrol2(patrol.getNO());
            patrolValue.setPatrol3(patrol.getEI());
            patrolValue.setPatrol4(patrol.getEO());
            patrolValue.setPatrol5(patrol.getHI());
            patrolValue.setPatrol6(patrol.getHO());
            patrolValue.setPatrol7(patrol.getDN());
            patrolValue.setPatrol8(patrol.getDE());
            patrolValue.setPatrol9(patrol.getDF());
            patrolValue.setPatrol10(patrol.getDH());
            patrolValue.setPatrol11(patrol.getDG());
            patrolValueMapper.updatePatrolValue(patrolValue);
        }
        return 0;
    }
}
